package com.mxprojects.gui
{
	import com.mxprojects.constructors.ButtonSmall;
	import com.mxprojects.constructors.InputConstructor;
	import com.mxprojects.constructors.SimpleTextField;
	import com.mxprojects.io.BoardCookie;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.ServerHandler;
	import com.mxprojects.io.TagParser;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.PrivateMessages;
	
	import flash.events.MouseEvent;
	
	public class PrivateViewMessage extends PrivateMessages
	{
		private static const MESSAGE_ROW_COUNT:int = 11;
		private static const MESSAGE_BORDER_ALPHA:Number = .5;
		
		private static var currentRecipientID:int;
		private static var currentRecipientStatus:int;
		private static var currentSenderID:int;
		private static var currentSenderStatus:int;
		private static var currentDisplayName:String;
		private static var currentSubject:String;
		private static var currentMessage:String;
		private static var currentDate:String;
		private static var currentReplied:String;
		private static var serverHandler:ServerHandler = new ServerHandler();
		private static var nextY:int;
		private static var deleteButton:ButtonSmall;
		
		public static function viewMessage(messageID:int, recipientID:int, recipientStatus:int, senderID:int, senderStatus:int, displayName:String, subject:String, message:String, date:String, replied:String):void {
			//this will take the user to an area where they can view the message
			setupHolder();
			
			//set the shared stuff
			currentMessageID = messageID;
			currentRecipientID = recipientID;
			currentRecipientStatus = recipientStatus;
			currentSenderID = senderID;
			currentSenderStatus = senderStatus;
			currentDisplayName = displayName;
			currentSubject = subject;
			currentMessage = message;
			currentDate = date;
			currentReplied = replied;		
			
			//this will help us incase we need to add/remove elements
			nextY = 0;
			
			//display a couple of textboxes
			var subjectText:SimpleTextField = new SimpleTextField("<span class=\"privateViewSubject\">" + subject + "</span>");
			subjectText.selectable = true;
			nextY += subjectText.height;
			holder.addChild(subjectText);
			
			var usernameText:SimpleTextField = new SimpleTextField("<span class=\"privateViewUsername\">" + displayName + "</span>");
			usernameText.y = nextY;
			nextY += usernameText.height + Config.TEXT_PADDING;
			holder.addChild(usernameText);
			
			//create a little text box to show when the message was sent/received
			var dateText:SimpleTextField = new SimpleTextField("<span class=\"privateViewDate\">" + Language.main(Word.DATE) + ": " + date + "</span>");
			dateText.y = usernameText.y;
			dateText.x = holder.width - dateText.width;
			holder.addChild(dateText);
			
			//if there is a reply, let's let them know when
			if(replied != "0"){
				var replyText:SimpleTextField = new SimpleTextField("<span class=\"privateViewReplyTime\">" + Language.main(Word.REPLIED) + ": " + replied + "</span>");
				replyText.y = dateText.y;
				replyText.x = holder.width - replyText.width;
				holder.addChild(replyText);
				
				//move the date text up above the reply
				dateText.y -= replyText.height;
			}
			
			//show the message
			var messageBox:InputConstructor = buildMessage(message);
						
			//add a back button
			var backButton:ButtonSmall = new ButtonSmall(Language.main(Word.BACK_TO) + " " + Language.main(currentLocation), backToLocation);
			backButton.x = messageBox.x + messageBox.inputBox.x;
			backButton.y = Math.round(nextY);
			holder.addChild(backButton);
			
			//add a button to reply to the message
			var replyButton:ButtonSmall;
			if(currentLocation == Word.INBOX){
				replyButton = new ButtonSmall(Language.main(Word.REPLY), replyCurrentMessage);
				replyButton.y = Math.round(nextY);
				replyButton.x = Math.round(messageBox.x + messageBox.inputLabel.width + messageBox.inputBox.width - replyButton.width);
				holder.addChild(replyButton);
			}
			
			//add a button to delete the message
			var permaDelete:String = "";
			if(currentLocation == Word.TRASH){
				//modifiy the button text if we are going to perma delete this message
				permaDelete = Language.main(Word.PERMANENTLY) + " ";
			}
			deleteButton = new ButtonSmall(permaDelete + Language.main(Word.DELETE), deleteCurrentMessage);
			
			//see if the reply button is there to offset from
			if(replyButton != null){
				deleteButton.x = replyButton.x - deleteButton.width - Config.TEXT_PADDING/2;
			}else{
				deleteButton.x = Math.round(messageBox.x + messageBox.inputLabel.width + messageBox.inputBox.width - deleteButton.width);
			}
			
			deleteButton.y = Math.round(nextY);
			holder.addChild(deleteButton);

			//mark the message as read if it's new
			if(recipientStatus == NEW && recipientID == int(BoardCookie.getUserID())){
				currentRecipientStatus = READ;
				updateStatus(READ);
			}
		}
		
		public static function showLastMessage():void {
			//this allows someone in another view to show the last message rendered
			viewMessage(currentMessageID, currentRecipientID, currentRecipientStatus, currentSenderID, currentSenderStatus, currentDisplayName, currentSubject, currentMessage, currentDate, currentReplied);
		}
		
		private static function buildMessage(message:String):InputConstructor { 
			//this will put the actual message on the pm popup
			var messageText:InputConstructor = new InputConstructor(Language.main(Word.MESSAGE), 
												"<span class=\"privateViewMessage\">" + TagParser.convertToHtml(message) + "</span>", 
												3, false, MESSAGE_ROW_COUNT, "formInputLabel", "formInput", 0, "", false);
			//var messageText:SimpleTextField = new SimpleTextField("<span class=\"privateViewMessage\">" + TagParser.convertToHtml(message) + "</span>", MESSAGE_ROW_COUNT, true, holder.width);
			messageText.y = nextY;
			messageText.changeWidth(holder.width - messageText.inputLabel.width);
			messageText.inputBox.selectable = true;
			nextY += messageText.height + Config.TEXT_PADDING;
			
			//put a background behind the message so it doesn't look like it's floating in space
//			var background:Sprite = new Sprite();
//			background.graphics.beginFill(Styles.colorToUint(".privateViewMessageBackground"));
//			background.graphics.drawRect(0,0, messageText.width, messageText.height);
//			background.graphics.endFill();
//			background.x = messageText.x;
//			background.y = messageText.y;
//			holder.addChild(background);
			
			//apply to the style from the CSS
//			Styles.setProperties(background, ".privateViewMessageBackground");
			
			//add the message after we are done making the box look pretty
			holder.addChild(messageText);
			
			//put a border around the bg
//			if(Styles.css.getStyle(".privateViewMessageBackground")["borderColor"] != undefined){
//				var box:Sprite = DrawBorder.create(background, Styles.colorToUint(".privateViewMessageBackground", "borderColor"));
//				box.x = background.x;
//				box.y = background.y;
//				holder.addChild(box);
//			}			
			
			//add a scroller to the message incase it needs it
			//var scroller:TextScroller = new TextScroller(messageText);
			//holder.addChild(scroller);
			
			return messageText;
		}
		
		private static function backToLocation(event:MouseEvent):void {
			//just sends them back to the location they just came from
			changeLocation(currentLocation, dataFromServer.currentPage);
		}
		
		private static function replyCurrentMessage(event:MouseEvent):void {
			//take the current message and reply to it!
			PrivateSendMessage.sendMessage(currentDisplayName, currentSubject, currentMessage, currentMessageID);
		}
		
		private static function deleteCurrentMessage(event:MouseEvent):void {
			//send the current message to the trash
			var status:int = TRASH;
			if(currentLocation == Word.TRASH){
				//delete the message forever if we are already in the trash
				status = DELETE;
			}
			
			//disable the delete button
			deleteButton.enabled = false;
			
			//send the message ID and then giv'r
			updateStatus(status);
		}
		
		private static function updateStatus(status:int):void {
			//this just updates the status of messages
			dataToSend.location = status;
			dataToSend.messageID = currentMessageID;
			dataToSend.senderID = currentSenderID;
			dataToSend.recipientID = currentRecipientID;
			
			serverHandler.transmitData("privatemessages.php", dataToSend);
			serverHandler.listenForData(processData);
		}
	}
}